﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace FormatXaml
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string path = string.Empty;
                if (args.Count() > 0)
                    path = Path.GetDirectoryName(args[0]);

                if (!Directory.Exists(path))
                    path = AppDomain.CurrentDomain.BaseDirectory;
                Console.WriteLine("path:" + path);
                string pattern = "*.xaml";
                foreach (string fileName in Directory.GetFiles(path, pattern, SearchOption.AllDirectories))
                {
                    //Console.WriteLine("fileName:" + fileName);
                    bool isModified = false;
                    try
                    {
                        string[] originalText = File.ReadAllLines(fileName, Encoding.UTF8);
                        StringBuilder output = new StringBuilder();
                        int currentIndent = 0;
                        foreach (string originalLine in originalText)
                        {
                            int leadingSpaces = 0;
                            int leadingTabs = 0;
                            foreach (char c in originalLine)
                            {
                                if (c == ' ')
                                {
                                    leadingSpaces++;
                                }
                                else
                                {
                                    if (c != '\t')
                                    {
                                        break;
                                    }
                                    leadingTabs++;
                                }
                            }
                            int indent = leadingSpaces + (2 * leadingTabs);
                            string formattedLine = originalLine.Trim(new char[] { ' ', '\t' });
                            if (formattedLine.StartsWith("<"))
                            {
                                currentIndent = indent;
                            }
                            else if (string.IsNullOrEmpty(formattedLine))
                            {
                                indent = 0;
                            }
                            else
                            {
                                indent = currentIndent + 4;
                            }
                            formattedLine = new string(' ', indent) + formattedLine;
                            if (formattedLine != originalLine)
                            {
                                isModified = true;
                            }
                            output.AppendLine(formattedLine);
                        }
                        if (isModified)
                        {
                            if (File.GetAttributes(fileName).HasFlag(FileAttributes.ReadOnly))
                            {
                                //Console.WriteLine(fileName + " is readonly, can't forma...");
                            }
                            else
                            {
                                Console.WriteLine("Format Xaml： " + fileName);
                                File.WriteAllText(fileName, output.ToString(), Encoding.UTF8);
                            }
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        if (!isModified)
                        {
                            throw;
                        }
                        //Console.WriteLine("Error writing output file! Need to checkout " + fileName + "?");
                    }
                    catch (IOException ex)
                    {
                        if (isModified)
                        {
                            //Console.WriteLine("Error writing output file! Need to checkout " + fileName);
                        }
                        else
                        {
                           // Console.WriteLine("Error reading " + fileName);
                           // Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            catch (ReadOnlyException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled exception");
                Console.WriteLine(ex.ToString());
            }

            //Console.WriteLine("Please press [Space] to exit.");
            //while (Console.ReadKey().Key != ConsoleKey.Spacebar)
            //{

            //}

        }
    }
}
